package org.example.erp.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.erp.domain.Notice;
import org.example.erp.service.impl.NoticeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class NoticeController {
    @Autowired
    private NoticeServiceImpl noticeServiceImpl;
    @GetMapping("/announcement/find")
    public DataResult findNotice(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                 @RequestParam(value = "pageSize",defaultValue = "2")Integer pageSize,
                                 @RequestParam(required = false) String noticeTitle,
                                 @RequestParam(required = false) String created,
                                 @RequestParam(required = false) String noticeType){
        LambdaQueryWrapper<Notice> noticeWrapper = new LambdaQueryWrapper<>();
        noticeWrapper.like(!"".equals(noticeTitle) && noticeTitle!=null, Notice::getNoticeTitle,noticeTitle)
                .like(!"".equals(created) && created!=null,Notice::getCreated,created)
                .eq(!"".equals(noticeType) && noticeType!=null,Notice::getNoticeType,noticeType);
        IPage<Notice> noticePage = new Page<>(currentPage,pageSize);
        IPage<Notice> result = noticeServiceImpl.page(noticePage, noticeWrapper);
        return DataResult.success(result);
    }
    @PostMapping("/announcement/insert")
    public DataResult insertNotice(@RequestBody Notice notice){
        noticeServiceImpl.save(notice);
        return DataResult.success();
    }
    @PutMapping("/announcement/update")
    public DataResult updateNotice(@RequestBody Notice notice){
        noticeServiceImpl.updateById(notice);
        return DataResult.success();
    }
    @DeleteMapping("/announcement/delete")
    public DataResult deleteNotice(@RequestBody List<Integer> noticeIds){
        noticeServiceImpl.removeByIds(noticeIds);
        return DataResult.success();
    }
}
